class Solution {
public:
    bool check(unordered_map<char,int>& hash1,unordered_map<char,int>& hash2){
        for(const auto& kv : hash2){
            char k = kv.first;
            int v = kv.second;
            if(!hash1.count(k) || hash1[k] < hash2[k]){
                return false;
            }
        }
        return true;
    }
                                        
    string minWindow(string s, string t) {
        int n = s.size();
        int m = t.size();
        
        if(n < m) return "";
        
        unordered_map<char,int> hash1,hash2;
        
        int i = 0;
        int j = -1;
        int Min = INT_MAX;
        
        for(int k = 0;k < m;k++){
            hash2[t[k]]++;
        }
        
        int l = 0;
        int r = 0;

        
        while(l < n && r <= n){
            bool ans = check(hash1,hash2);
            if(ans){
                if(r-l < Min){
                    i = l;
                    j = r-1;
                    Min = min(Min,j-i+1);
                }
                hash1[s[l]]--;
                l++;
            }else{
                if(r < n){
                    hash1[s[r]]++;
                }
                r++;
            }
        }
        return s.substr(i,j-i+1);
    }
};
